﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Zhaoxi.CourseManagement.Common;
using Zhaoxi.CourseManagement.Model;

namespace Zhaoxi.CourseManagement.ViewModel
{
    public class MainViewModel : NotifyBase
    {
        public UserModel UserInfo { get; set; }

        private FrameworkElement _mainContent;

        public FrameworkElement MainContent
        {
            get { return _mainContent; }
            set 
            { 
                _mainContent = value; 
                this.DoNotify();
            }
        }

        //导航切换命令
        public CommandBase NavChangedCommand { get; set; }

        //导航命令初始化
        public MainViewModel()
        {
            //在UserInfo使用之前初始化
            UserInfo = new UserModel();

            this.NavChangedCommand = new CommandBase();
            this.NavChangedCommand.DoExecute = new Action<object>(DoNavChanged);
            this.NavChangedCommand.DoCanExecute = new Func<object, bool>((o) => true);
        }

        //通过obj参数，通过反射，把这个对象设置到MainContent里，从而实现跳转
        private void DoNavChanged(object obj)
        {
            Type type = Type.GetType("Zhaoxi.CourseManagement.View." + obj.ToString());
            ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
            this.MainContent = (FrameworkElement)cti.Invoke(null);
        }
    }
}
